TARGET_NAME = ZINO_GuoLiForkLift
TARGET_BOARD = GuoLiForkLift
SERIAL_PROT = COM15
MCPU = AT32F421C8T7
BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 1
SUB_VERSION := 0
BUILD_NUMBER := 0
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)_$(BUILD_DATE)
TARGET_ELF = ./build/$(TARGET_NAME)_$(BUILD_VERSION).elf
TARGET_BIN = ./build/$(TARGET_NAME)_$(BUILD_VERSION).bin
FIRMWARE = ./$(TARGET_BOARD)_$(TARGET_NAME)_$(BUILD_VERSION).bin
ROOT     		:= $(patsubst %/,%,$(dir $(lastword $(MAKEFILE_LIST))))
targets: $(zino_tops_lib) $(TARGET_BIN) $(TARGET_ELF) $(nOBJS)

# include ./Debug/makefile

# -include ./Debug/sources.mk
submks= $(addprefix $(ROOT)/Debug/, $(SUBDIRS))
submks2= $(addsuffix /subdir.mk, $(submks))
# $(info submks: $(submks2))
-include $(submks2)

c_prefix=../%
s_prefix=../%
d_prefix=./%
o_prefix=./%
nc_prefix=./%
ns_prefix=./%
nd_prefix=./build/%
no_prefix=./build/%

nC_SRCS =$(patsubst $(c_prefix),$(nc_prefix),$(C_SRCS))
nC_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(C_DEPS))
nOBJS =$(patsubst $(o_prefix),$(no_prefix),$(OBJS))
nS_SRCS =$(patsubst $(s_prefix),$(ns_prefix),$(S_SRCS))
nS_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(S_DEPS))
nS_UPPER_SRCS =$(patsubst $(s_prefix),$(ns_prefix),$(S_UPPER_SRCS))
nS_UPPER_DEPS =$(patsubst $(d_prefix),$(nd_prefix),$(S_UPPER_DEPS))


# include ./applications/ZINO_FC_V4.0/zino_fc.mk
# $(info nC_SRCS: $(nC_SRCS))aq	WQ2W

# $(info ROOT: $(ROOT))
# NOTE 所有头文件路径！
c_flags = \
	-DARDUINO -DZINO_RC_HOPPING\
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections \
	-Wall  -g -gdwarf-2 -D$(MCPU)\
	-lm -DARM_MATH_CM4 \
	-u _printf_float \
	-DDEV_MVID=$(MAIN_VERSION) -DDEV_SVID=$(SUB_VERSION) -DDEV_BVID=$(BUILD_NUMBER) \
	-I"$(ROOT)\drivers" \
	-I"$(ROOT)\drivers\f421" \
	-I"$(ROOT)\libraries\f421\cmsis\cm4\core_support" \
	-I"$(ROOT)\libraries\f421\cmsis\cm4\device_support" \
	-I"$(ROOT)\libraries\f421\drivers\inc" \
	-I"$(ROOT)" \
	-I"$(ROOT)\applications" \
	-I"$(ROOT)\rt-thread\components\finsh" \
	-I"$(ROOT)\rt-thread\include" \
	-I"$(ROOT)\rt-thread\libcpu\arm\common" \
	-I"$(ROOT)\rt-thread\libcpu\arm\cortex-m4" \
	-include"$(ROOT)/rtconfig_preinc.h" \
	-std=gnu11 -MMD -MP -MD -MF "$(@:%.o=%.d)" -MT"$(@)"

include ./applications/app.mk
# $(info c_flags: $(c_flags))
zino_tops_lib = $(ROOT)/build/guoli.a
math_lib = libarm_cortexM4l_math
math_lib_dir = $(ROOT)/libraries/cmsis/lib/gcc/
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")
# NOTE -lc -lm 链接数学库时需要！!！最后！！！再链接，注意顺序
asm_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp \
	-I"$(ROOT)" \
	-Xassembler -mimplicit-it=thumb -MMD -MP

-include $(nC_DEPS)

./build/%.o: %.c
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $< -c -g -o $@ $(c_flags)

# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2 -x assembler-with-cpp -I"D:\RT-ThreadStudio\workspace\ZINO_FC_V4" -Xassembler -mimplicit-it=thumb -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.s
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.S
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"

$(arduino_obj): $(arduino_src)
	@ echo "$(DATE_TIME) building arduino files $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(c_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
# $(info nbojs:$(nOBJS))
$(zino_tops_lib): $(nOBJS)
	@ echo "$(DATE_TIME) building static library $@  ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-ar rcs $@ $(nOBJS)
m_ldflags= \
	-mcpu=cortex-m4 \
	-mthumb -mfloat-abi=soft -Os \
	-Wall  -g -gdwarf-2 \
	-T "$(ROOT)\linkscripts\AT32F421C8\link.lds" \
	-Wl,-Map,"./build/$(TARGET_NAME).map" \
	-Xlinker --gc-sections \
	-Xlinker --cref -Wl,--print-memory-usage

# arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O0 -ffunction-sections -fdata-sections -Wall  -g -gdwarf-2
# -T "D:\RT-ThreadStudio\workspace\ZINO_FC_V4\linkscripts\AT32F413RC\link.lds"
#  -Xlinker --gc-sections -Wl,-Map,"rtthread.map"
# -Xlinker --cref -o "rtthread.elf" $(OBJS) $(USER_OBJS) $(LIBS)
# $(TARGET_ELF): $(zino_tops_lib)
# 	@ echo "$(DATE_TIME) Linking everyting together... $@"
# 	@ arm-none-eabi-gcc $(m_ldflags) -o $(TARGET_ELF) $(nOBJS) -lc -lm
# 	@ arm-none-eabi-size $(TARGET_ELF)
$(TARGET_ELF): $(nOBJS)
	@ echo "$(DATE_TIME) Linking everyting together... $@"
	@ arm-none-eabi-gcc $(m_ldflags) -lc -lm -o $(TARGET_ELF) $(nOBJS)

cl:
	-$(RM) $(nOBJS)$(TARGET_ELF)$(nC_DEPS) ./build
	-@echo ' '
$(TARGET_BIN): $(TARGET_ELF)
	@ echo "$(DATE_TIME) Create binary file... $@"
	@ arm-none-eabi-objcopy -O binary $(TARGET_ELF) $(TARGET_BIN)

$(FIRMWARE): $(TARGET_BIN)
	@ echo "make firmare"
	./tool/makeFirmware.exe $(TARGET_BIN) $(FIRMWARE)
IAP_flash_$(TARGET): $(TARGET_BIN)
	chcp 65001
	./UploadTool/ZINO_IAP_WIN_V1.2.exe --serialdev $(SERIAL_PROT) --board $(TARGET_BOARD) --mcu $(MCPU) --id --version --write 0x08001800 --loadfile $(TARGET_BIN)
download: $(FIRMWARE) $(TARGET_BIN)  $(TARGET_ELF)
	./tool/at32_iap_tool_win_v3.exe $(SERIAL_PROT) 921600 $(FIRMWARE)
dl: download

mix:
	cp $(zino_tops_lib) $(TARGET_MIXLYDIR)
	echo "$(DATE_TIME) Copy library to $(TARGET_MIXLYDIR) ..."



